import Vue from "vue";
import Router from "vue-router";
import Layout from "../layout/index.vue";
import { getToken } from "@/utils/auth.js";

Vue.use(Router);

export const constantRoutes = [
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login/index.vue"),
    hidden: true,
  },

  {
    path: "/404",
    name: "404",
    component: () => import("../views/404.vue"),
    hidden: true,
  },

  {
    path: "/",
    component: Layout,
    redirect: "/index",
    children: [
      {
        path: "index",
        name: "index",
        component: () => import("../views/dashboard/index.vue"),
        meta: { title: "首页", icon: "dashboard" },
      },
    ],
  },

  {
    path: "/user",
    component: Layout,
    name: "user",
    redirect: "/user/vip",
    meta: {
      title: "账号管理",
      icon: "el-icon-s-custom",
    },
    children: [
      {
        path: "vip",
        name: "vipSetting",
        component: () => import("../views/user/vip.vue"),
        meta: { title: "会员账号管理", icon: "el-icon-user-solid" },
      },
      {
        path: "account",
        name: "account",
        component: () => import("../views/user/account.vue"),
        meta: { title: "内部账号管理", icon: "el-icon-s-cooperation" },
      },
    ],
  },

  {
    path: "/directory",
    component: Layout,
    name: "directory",
    redirect: "/directory/setting",
    meta: {
      title: "内容管理",
      icon: "el-icon-setting",
    },
    children: [
      {
        path: "banner",
        name: "bannerSetting",
        component: () => import("../views/banner/index.vue"),
        meta: { title: "banner设置", icon: "el-icon-picture-outline" },
      },
      {
        path: "download",
        name: "downloadSetting",
        component: () => import("../views/download/index.vue"),
        meta: { title: "下载列表", icon: "el-icon-folder-opened" },
      },
      {
        path: "setting",
        name: "directorySetting",
        component: () => import("../views/directory/directory-setting.vue"),
        meta: { title: "栏目设置", icon: "el-icon-s-cooperation" },
      },
      {
        path: "article",
        name: "directory-article",
        component: () => import("../views/directory/directory-list.vue"),
        meta: { title: "栏目文章", icon: "el-icon-tickets" },
      },
      {
        path: "article-edit",
        name: "content-article-edit",
        component: () => import("../views/directory/directory-article.vue"),
        hidden: true,
        meta: { title: "栏目文章-编辑", activeMenu: "/directory/article" },
      },
    ],
  },

  {
    path: "/topic",
    component: Layout,
    name: "topic",
    redirect: "/topic/list",
    meta: {
      title: "职业培训",
      icon: "el-icon-s-management",
    },
    children: [
      {
        path: "index",
        name: "topicIndex",
        component: () => import("../views/topic/list.vue"),
        meta: { title: "主题列表", icon: "el-icon-s-promotion" },
      },
      {
        path: "certificateConfig",
        name: "topicCertificateConfig",
        component: () => import("../views/topic/certificate-config/index.vue"),
        meta: { title: "证书模板配置", icon: "el-icon-s-opportunity" },
      },

      {
        path: "person-certificates",
        name: "personCertificates",
        component: () => import("../views/topic/person-certificates/index.vue"),
        meta: { title: "个人证书", icon: "el-icon-medal" },
      },

      {
        path: "enterprise-certificates",
        name: "enterpriseCertificates",
        component: () =>
          import("../views/topic/enterprise-certificates/index.vue"),
        meta: { title: "企业证书", icon: "el-icon-trophy" },
      },
      {
        path: "teacher",
        name: "topicTeacher",
        component: () => import("../views/topic/teacher/index.vue"),
        hidden: true,
        meta: {
          title: "讲师简介",
          icon: "el-icon-s-custom",
          activeMenu: "/topic/index",
        },
      },
      {
        path: "material",
        name: "topicMaterial",
        component: () => import("../views/topic/material/index.vue"),
        hidden: true,
        meta: {
          title: "材料管理",
          icon: "el-icon-s-custom",
          activeMenu: "/topic/index",
        },
      },
      {
        path: "lesson",
        name: "topicLesson",
        component: () => import("../views/topic/lesson/index.vue"),
        hidden: true,
        meta: {
          title: "课程管理",
          icon: "el-icon-s-custom",
          activeMenu: "/topic/index",
        },
      },
      {
        path: "lesson-video",
        name: "topicVideo",
        component: () => import("../views/topic/lesson/video/index.vue"),
        hidden: true,
        meta: {
          title: "课程-视频",
          icon: "el-icon-s-custom",
          activeMenu: "/topic/index",
        },
      },
      {
        path: "lesson-question-bank",
        name: "topicQuestionBank",
        component: () =>
          import("../views/topic/lesson/question-bank/index.vue"),
        hidden: true,
        meta: {
          title: "课程-题库",
          icon: "el-icon-s-custom",
          activeMenu: "/topic/index",
        },
      },

      {
        path: "lesson-person",
        name: "topicPerson",
        component: () => import("../views/topic/lesson/person/index.vue"),
        hidden: true,
        meta: {
          title: "课程-会员",
          icon: "el-icon-s-custom",
          activeMenu: "/topic/index",
        },
      },
    ],
  },

  {
    path: "/award",
    component: Layout,
    name: "award",
    redirect: "/award/list",
    meta: {
      title: "评选表彰",
      icon: "el-icon-help",
    },
    children: [
      {
        path: "list",
        name: "award-list",
        component: () => import("../views/award/award-list/index.vue"),
        meta: { title: "评选表彰信息", icon: "el-icon-star-on" },
      },
      {
        path: "kind",
        name: "award-kind",
        component: () => import("../views/award/award-kind/index.vue"),
        meta: { title: "表彰分类", icon: "el-icon-s-shop" },
      },
    ],
  },

  { path: "*", redirect: "/404", hidden: true },
];

const createRouter = () =>
  new Router({
    scrollBehavior: () => ({ y: 0 }),
    base: "/manager/",
    mode: "hash",
    routes: constantRoutes,
  });

const router = createRouter();

router.beforeEach(async (to, from, next) => {
  if (to.path === "/login") {
    next();
    return;
  }
  try {
    if (getToken()) {
      if (to.path === "/login") {
        next({ path: "/login" }); // 重定向至主页或应用中合适的其他路由
      } else {
        next(); // 继续前往目标路由
      }
    } else {
      next({ path: "/login" });
    }
  } catch (error) {
    // 如果校验失败，重定向到登录页面
    next({ path: "/login" });
  }
});

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
